<template>
  <div>
    <mbao-x item1="商品管理" item2="商品列表"></mbao-x>
    <el-card class="box-card Top">
      <el-row :gutter="12">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="forms.query"
            class="input-with-select"
            @input="Search"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
          <div class="grid-content bg-purple"></div
        ></el-col>
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <el-button type="primary" @click="Open">添加商品</el-button>
          </div></el-col
        >
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%" class="Top">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="goods_price" label="邮箱"> </el-table-column>
        <el-table-column prop="goods_weight" label="电话"> </el-table-column>
        <el-table-column prop="add_time" label="状态">
          <template slot-scope="scope">
            {{ scope.row.add_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="OpenEdit(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="Del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="Top"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="forms.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="forms.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import _ from 'lodash'
import MbaoX from '../components/MbaoX.vue'
export default {
  components: {
    MbaoX
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      //   获取商品列表数据
      forms: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getlist()
  },
  mounted() {},
  methods: {
    async getlist() {
      const res = await this.$http.Shop(this.forms)
      console.log(res)
      this.total = res.total
      this.tableData = res.goods
    },
    // 编辑
    OpenEdit(row) {
      console.log(row)
      this.$router.push({
        path: '/addgoods',
        query: row
      })
    },
    // 删除
    async Del(row) {
      console.log(row)
      let Dels
      try {
        Dels = await this.$confirm(
          '此操作将永久删除该文件, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
      } catch (error) {
        Dels = error
      }
      if (Dels === 'confirm') {
        const res = this.$http.delGood(row.goods_id)
        console.log(res)
        this.getlist()
      }
    },
    Open() {
      this.$router.push('/addgoods')
    },
    // // 点击确定
    // submitForm(formName) {
    //   this.$refs[formName].validate(async (valid) => {
    //     if (valid) {
    //       if (this.names === '添加用户') {
    //         const res = await this.$http.AddUsers(this.Form)
    //         console.log(res)
    //       } else {
    //         const res = await this.$http.EditUsers(this.EditID, this.Form)
    //         console.log(res)
    //       }
    //       this.getlist()
    //       this.dialogFormVisible = false
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
    // },
    // 利用lodash 实现节流 搜索功能
    Search: _.throttle(function () {
      this.getlist()
    }, 3000),
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.forms.pagesize = val
      this.getlist()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.forms.pagenum = val
      this.getlist()
    }
  }
}
</script>
<style scoped lang='scss'>
.Top {
  margin-top: 20px;
}
</style>
